// pages/exam/exam.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      flag:'0',  //题目是否完成
      isWrong:"0",
      tap:'',
      index:1,    //题号
      submit:"",
      examId:"",
      title:"以下哪个说法正确？",
      answerA:"",
      answerB:"",
      answerC:"",
      answerD:"",
      realAnswer:"",
      chapter:"",
      conceal:"true",
      isComplete:false,
      wrongQuestionDate:"",
      projectId:"",
      count:"",
      wrongId:1,
      projectSelectId:"",
      chapterId:"",
      chapterList:["函数、极限与连续", "一元函数微分学", "一元函数积分学", "向量代数和空间解析几何", "多元函数微分学", "多元函数积分学", "无穷级数"],
      isImage:"",
      IntelligenceList:[]
  },

  init:function(){
    this.setData({
      flag:'0',  //题目是否完成
      isWrong:"0",
      tap:'',
      index:1,    //题号
      submit:"",
      title:"以下哪个说法正确？",
      answerA:"",
      answerB:"",
      answerC:"",
      answerD:"",
      realAnswer:"",
      chapter:"",
      conceal:"true",
      isComplete:false,
      wrongQuestionDate:"",
      projectId:"",
      count:"",
      wrongId:1,
      projectSelectId:"",
      chapterId:"",
      chapterList:["函数、极限与连续", "一元函数微分学", "一元函数积分学", "向量代数和空间解析几何", "多元函数微分学", "多元函数积分学", "无穷级数"],
      isImage:"",
    })
  },

  change:function(e){
    var _this = this
    var index = e.target.dataset.item;
    var flag = this.isComplete
    this.setQuestionSolvedCount();
    this.addChapterCount();
    if(!flag){
      this.setData({
        tap : index,
        submit: index,
        conceal:"false",
      })
      this.isComplete = !flag
    }
    if(this.data.submit!=this.data.realAnswer){
      this.addWrong();
      this.addChapterWrongCount();
    }
    this.setWrongRate()
  },

  addChapterCount:function(){
    var _this = this
    wx.request({
      url: 'http://localhost:8081/exam/updateChapterCount',
      data:{projectId:_this.data.projectId,chapterId:_this.data.chapterId},
      success:function(res){
        console.log("更新章节题目数量成功");
      }
    })
  },

  addChapterWrongCount:function(){
    var _this = this
    wx.request({
      url: 'http://localhost:8081/exam/updateChapterWrongCount',
      data:{projectId:_this.data.projectId,chapterId:_this.data.chapterId},
      success:function(res){
        console.log("更新章节错题数量成功");
      }
    })
  },

  setWrongRate:function(){
    var projectSelectId = this.data.projectSelectId
    var projectId = this.data.projectId
    wx.request({
      url: 'http://localhost:8081/exam/setWrongRate',
      data:{projectId:projectId,projectSelectId:projectSelectId},
      success:function(res){
        console.log("更新正确率成功");
      }
    })
  },

  updateWrongQuestionSolved:function(){
    var _this = this
    wx.request({
      url: 'http://localhost:8081/exam/updateWrongQuestionSolvedCount',
      data:{projectId:_this.data.projectId,projectSelectId:_this.data.projectSelectId},
      success:function(res){
        console.log("更新解决错题数量成功");
      }
    })
  },

  front:function(){
    if(this.data.isWrong=="0"){
      this.isComplete = false
      this.data.index--;
      var index = this.data.index
      this.data.submit = ""
      this.setData({
        tap:"",
        conceal:"true",
        index:index
      })
      this.getQuestionById(this.data.projectId);
      var submit = this.data.submit
      this.setData({
        tab:submit
      })
    }else if(this.data.isWrong=="1"){
      this.isComplete = false
      this.data.wrongId--;
      this.data.submit = ""
      this.setData({
        tap:"",
        conceal:"true",
      })
      this.getWrongQuestion(304);
      var submit = this.data.submit
      this.setData({
        tab:submit
      })
    }else if(this.data.isWrong=="2"){
      this.isComplete = false
      this.data.index--;
      this.data.submit = ""
      this.setData({
        tap:"",
        conceal:"true"
      })
      this.getWrongQuestion();
      var submit = this.data.submit
      this.setData({
        tab:submit
      })
    }else if(this.data.isWrong=="3"){
      this.isComplete = false
      this.data.submit = ""
      this.setData({
        tap:"",
        conceal:"true"
      })
      this.getIntelligentQuestion();
      var submit = this.data.submit
      this.setData({
        tab:submit
      })
    }
  },

  next:function(){
    if(this.data.isWrong=="0"){
      this.isComplete = false
      this.data.index++;
      var index = this.data.index
      this.data.submit = ""
      this.setData({
        tap:"",
        conceal:"true",
        index:index
      })
      this.getQuestionById(this.data.projectId);
      var submit = this.data.submit
      this.setData({
        tab:submit
      })
    }else if(this.data.isWrong=="1"){
      this.isComplete = false
      this.data.wrongId++;
      var index = this.data.wrongId
      this.data.submit = ""
      this.setData({
        tap:"",
        conceal:"true",
        index:index
      })
      this.getWrongQuestion();
      var submit = this.data.submit
      this.setData({
        tab:submit
      })
    }else if(this.data.isWrong=="2"){
      this.isComplete = false
      this.data.index++;
      this.data.submit = ""
      this.setData({
        tap:"",
        conceal:"true"
      })
      this.getChapterQuestion();
      var submit = this.data.submit
      this.setData({
        tab:submit
      })
    }else if(this.data.isWrong=="3"){
      this.isComplete = false
      this.data.submit = ""
      this.setData({
        tap:"",
        conceal:"true"
      })
      this.setData({
        examId:this.data.IntelligenceList[this.data.index].id,
        title:this.data.IntelligenceList[this.data.index].title,
        answerA:this.data.IntelligenceList[this.data.index].answerA,
        answerB:this.data.IntelligenceList[this.data.index].answerB,
        answerC:this.data.IntelligenceList[this.data.index].answerC,
        answerD:this.data.IntelligenceList[this.data.index].answerD,
        submit:this.data.IntelligenceList[this.data.index].submit,
        realAnswer:this.data.IntelligenceList[this.data.index].realAnswer,
        chapterId:this.data.IntelligenceList[this.data.index].chapterId,
        isImage:this.data.IntelligenceList[this.data.index].isImage,
      })
      this.data.index++;
      var submit = this.data.submit
      this.setData({
        index:this.data.index,
        tab:submit
      })
    }
  },

  addWrong:function(){
    var _this = this
    this.updateWrongQuestionSolved()
      wx.request({
        url: 'http://localhost:8081/exam/insertWrong',
        data:{projectId:_this.data.projectId,examId:_this.data.index,projectSelectId:_this.data.projectSelectId,id:_this.data.examId},
        method:'get',
        success:function(res){
          console.log("插入错题本成功");
        }
      })
  },

  setQuestionSolvedCount:function(){
    var _this = this
    wx.request({
      url: 'http://localhost:8081/exam/setQuestionSolvedCount',
      data:{projectId:_this.data.projectId,projectSelectId:_this.data.projectSelectId},
      success:function(res){
        console.log("更新解决题目数量成功");
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  getIntelligentQuestion:function(){
    var _this = this
    wx.request({
      url: 'http://localhost:8081/exam/getIntelligence',
      data:{projectId:_this.data.projectId,projectSelectId:_this.data.projectSelectId},
      success:function(res){
        console.log(res.data);
        _this.data.IntelligenceList = res.data
        var index = 0
        console.log("获取智能推荐题目成功");
        _this.setData({
          examId:res.data[index].id,
          title:res.data[index].title,
          answerA:res.data[index].answerA,
          answerB:res.data[index].answerB,
          answerC:res.data[index].answerC,
          answerD:res.data[index].answerD,
          submit:res.data[index].submit,
          realAnswer:res.data[index].realAnswer,
          chapterId:res.data[index].chapterId,
          isImage:res.data[index].isImage,
        })
        wx.request({
          url: 'http://localhost:8081/exam/getChapterList',
          data:{projectId:_this.data.projectId,projectSelectId:_this.data.projectSelectId},
          success:function(res){
            var chapterId = _this.data.chapterId-1
            _this.setData({
              chapter:res.data[chapterId]
            })
          }
        })
      }
    })
  },

  getWrongQuestion:function(){
    var _this = this
        wx.request({
          url: 'http://localhost:8081/exam/getWrongQuestion',
          data:{projectId:_this.data.projectId,date:_this.data.wrongQuestionDate,id:_this.data.wrongId,projectSelectId:_this.data.projectSelectId},
          method:"get",
          success:function(res){
            console.log("获取错题成功");
            _this.setData({
              examId:res.data.data.id,
              title:res.data.data.title,
              answerA:res.data.data.answerA,
              answerB:res.data.data.answerB,
              answerC:res.data.data.answerC,
              answerD:res.data.data.answerD,
              submit:res.data.data.submit,
              realAnswer:res.data.data.realAnswer,
              chapterId:res.data.data.chapterId,
              isImage:res.data.data.isImage
            })
            wx.request({
              url: 'http://localhost:8081/exam/getChapterList',
              data:{projectId:_this.data.projectId,projectSelectId:_this.data.projectSelectId},
              success:function(res){
                var chapterId = _this.data.chapterId-1
                _this.setData({
                  chapter:res.data[chapterId]
                })
              }
            })
          }
        })
  },

  getQuestionCount:function(count){
    var _this = this
        _this.setData({
          count:count
        })
  },

  getWrongQuestionCount:function(){
    var _this = this
    wx.request({
      url: 'http://localhost:8081/exam/getWrongQuestionCount',
      data:{projectId:_this.data.projectId,projectSelectId:_this.data.projectSelectId,date:_this.data.wrongQuestionDate},
      success:function(res){
        _this.setData({
          count:res.data.data
        })
      }
    })
  },

  getQuestionById:function(projectId){
    var _this = this
    wx.request({
      url: 'http://localhost:8081/exam/get',
      data:{projectId:projectId,examId:_this.data.index,projectSelectId:_this.data.projectSelectId},
      method:'get',
      success:function(res){
        console.log(res.data.data);
        _this.setData({
          examId:res.data.data.id,
          title:res.data.data.title,
          answerA:res.data.data.answerA,
          answerB:res.data.data.answerB,
          answerC:res.data.data.answerC,
          answerD:res.data.data.answerD,
          realAnswer:res.data.data.realAnswer,
          chapterId:res.data.data.chapterId,
          isImage:res.data.data.isImage
        })
        wx.request({
          url: 'http://localhost:8081/exam/getChapterList',
          data:{projectId:_this.data.projectId,projectSelectId:_this.data.projectSelectId},
          success:function(res){
            console.log(res.data);
            var chapterId = _this.data.chapterId-1
            _this.setData({
              chapter:res.data[chapterId]
            })
          }
        })
      }
    })
  },

  getChapterQuestion:function(){
    var _this = this
    wx.getStorage({
      key:"data",
      success:function(res){
        wx.request({
          url: 'http://localhost:8081/exam/getChapterQuestion',
          data:{projectId:_this.data.projectId,projectSelectId:_this.data.projectSelectId,chapterId:_this.data.chapterId,questionId:_this.data.index},
          success:function(res){
            console.log(res.data);
            _this.setData({
              examId:res.data.id,
              title:res.data.title,
              answerA:res.data.answerA,
              answerB:res.data.answerB,
              answerC:res.data.answerC,
              answerD:res.data.answerD,
              realAnswer:res.data.realAnswer,
              chapterId:res.data.chapterId,
              isImage:res.data.isImage
            })
            wx.request({
              url: 'http://localhost:8081/exam/getChapterList',
              data:{projectId:_this.data.projectId,projectSelectId:_this.data.projectSelectId},
              success:function(res){
                var chapterId = _this.data.chapterId-1
                _this.setData({
                  chapter:res.data[chapterId]
                })
              }
            })
          }
        })
      }
    })
  },

  getChapterCount:function(count){
    var _this = this
        _this.setData({
          count:count
        })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init()
    var _this = this
    wx.getStorage({
      key:"data",
      success:function(res){
        _this.data.isWrong = res.data[0]
        _this.data.projectId = res.data[1]
        _this.data.projectSelectId = res.data[2]
        if(_this.data.isWrong=="0"){ 
          _this.getQuestionById(_this.data.projectId)
          _this.getQuestionCount(842)
        }else if(_this.data.isWrong=="1"){
          _this.data.wrongQuestionDate = res.data[3]
          _this.getWrongQuestion()
          _this.getWrongQuestionCount(304)
        }else if(_this.data.isWrong=="2"){
          _this.data.chapterId = res.data[3]
          _this.getChapterQuestion()
          _this.getChapterCount(87)
        }else if(_this.data.isWrong=="3"){
          _this.getIntelligentQuestion()
          _this.setData({
            In:"--",
            count:"20"
          })
        }
        // _this.setQuestionSolvedCount();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})